package com.slfx.data.permission.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slfx.data.permission.entity.*;
import com.slfx.data.permission.service.SystemAssetService;
import com.slfx.data.permission.service.SystemDeptService;
import com.slfx.data.permission.service.SystemUsersService;
import com.slfx.data.permission.service.UserDataAuthService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: slfx
 * @Date: 2022-12-31
 * @Description:
 **/
@RestController
@RequestMapping("/systemAsset")
public class SystemAssetController {

    @Resource
    private SystemAssetService systemAssetService;
    @Resource
    private SystemDeptService systemDeptService;
    @Resource
    private SystemUsersService systemUsersService;
    @Resource
    private UserDataAuthService userDataAuthService;

    /**
     * 根据ID获取
     *
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public SystemAsset getById(@RequestParam("id") Long id) {
        SystemAsset asset = systemAssetService.getById(id);
        return asset;
    }

    /**
     * 新增
     *
     * @param asset
     * @return
     */
    @PostMapping("/save")
    public Long save(@RequestBody SystemAsset asset) {
        systemAssetService.save(asset);
        return asset.getId();
    }

    /**
     * 获取分页数据
     *
     * @param asset
     * @return
     */
    @GetMapping("/page")
    public IPage<SystemAsset> page(HttpServletRequest request, SystemAsset asset,
                                   @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SystemAsset> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SystemAsset> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(asset.getAssetCode())) {
            queryWrapper.like("asset_code", asset.getAssetCode());
        }
        if (StringUtils.isNotBlank(asset.getAssetName())) {
            queryWrapper.like("asset_name", asset.getAssetName());
        }
        //一般从请求头或者上下文中获取到当前用户：获取到当前用户的部门
        String userId = request.getHeader("userId");
        if (StringUtils.isBlank(userId)) {
            return new Page<>(pageNo, pageSize);
        }
        //获取到当前用户的部门
        SystemUsers users = systemUsersService.getById(Long.valueOf(userId));
        // 查看逻辑：目前是能查看到当前部门和当前部门的子部门的资产
        List<SystemDept> childrenByParentId = systemDeptService.getChildrenByParentId(users.getDeptId());
        if (CollectionUtil.isNotEmpty(childrenByParentId)) {
            List<Long> deptIds = childrenByParentId.stream().map(SystemDept::getId).distinct().collect(Collectors.toList());
            queryWrapper.in("dept_id", deptIds);
        }
        IPage<SystemAsset> assetIPage = systemAssetService.page(page, queryWrapper);
        return assetIPage;
    }

    private static final String DEPT_DATA_CODE = "D20190428054";

    /**
     * 通用的数据权限：获取分页数据
     *
     * @param asset
     * @return
     */
    @GetMapping("/queryPage")
    public IPage<SystemAsset> queryPage(HttpServletRequest request, SystemAsset asset,
                                        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SystemAsset> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SystemAsset> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(asset.getAssetCode())) {
            queryWrapper.like("asset_code", asset.getAssetCode());
        }
        if (StringUtils.isNotBlank(asset.getAssetName())) {
            queryWrapper.like("asset_name", asset.getAssetName());
        }
        //一般从请求头或者上下文中获取到当前用户：获取到当前用户的项目权限
        String userId = request.getHeader("userId");
        if (StringUtils.isBlank(userId)) {
            return new Page<>(pageNo, pageSize);
        }
        //获取到当前用户的项目
        List<UserDataAuth> list = userDataAuthService.list(new LambdaQueryWrapper<UserDataAuth>()
                .eq(UserDataAuth::getDataCode, DEPT_DATA_CODE)
                .eq(UserDataAuth::getUserCode, userId));
        List<String> deptIds = list.stream().map(UserDataAuth::getAuthCode).distinct().collect(Collectors.toList());
        if (CollectionUtil.isEmpty(deptIds)) {
            return new Page<>(pageNo, pageSize);
        }
        queryWrapper.in("dept_id", deptIds);
        IPage<SystemAsset> projectAssetIPage = systemAssetService.page(page, queryWrapper);
        return projectAssetIPage;
    }
}
